package com.cloud.bssp.admin.wanda.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bssp.admin.wanda.entity.ClassesDO;
import com.cloud.bssp.admin.wanda.dto.ClassesDTO;
import com.cloud.bssp.admin.wanda.service.ClassesService;
import com.cloud.bssp.admin.wanda.convert.ClassesDoConvert;
import com.cloud.bssp.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 班次
 * Create Date: 2021-07-04T18:48:50.805
 * Modified By：<br>
 * Modified Date：<br>
 * Why & What is modified：<br>
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "班次")
@RestController
@RequestMapping("/classes")
@AllArgsConstructor
public class ClassesController {

    /**
     * ClassesService
     */
    private ClassesService classesService;

    /**
     * 分页列表
     * @param classesDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public R pageList(@RequestBody ClassesDTO classesDTO) {
        QueryWrapper<ClassesDO> queryWrapper = new QueryWrapper<>(ClassesDoConvert.dtoToDo(classesDTO));
        Page<ClassesDO> page = new Page<>(classesDTO.getPage(), classesDTO.getLimit());
        Page<ClassesDO> pageList = classesService.page(page, queryWrapper);
        return R.success(ClassesDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     * @param classesDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @PostMapping("/list")
    public R list(@RequestBody ClassesDTO classesDTO) {
        QueryWrapper<ClassesDO> queryWrapper = new QueryWrapper<>(ClassesDoConvert.dtoToDo(classesDTO));
        List<ClassesDO> classesList = classesService.list(queryWrapper);
        return R.success(ClassesDoConvert.listConvert(classesList));
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public R info(@RequestParam("id")Long id) {
            ClassesDO classes = classesService.getById(id);
        return R.success(ClassesDoConvert.doToDto(classes));
    }

    /**
     * 新增
     * @param classesDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody ClassesDTO classesDTO) {
        boolean flag = classesService.save(ClassesDoConvert.dtoToDo(classesDTO));
        if (flag) {
            return R.success();
        }
        return R.failed();
    }

    /**
     * 更新
     * @param classesDTO
     * @return
     */
    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public R update(@RequestBody ClassesDTO classesDTO) {
        boolean flag = classesService.updateById(ClassesDoConvert.dtoToDo(classesDTO));
        if (flag) {
            return R.success();
        }
        return R.failed();
    }
}